/*************************************************
 *FileName:      newfriend.js
 *Description:   新的朋友.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'newfriend');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var ohtml = '';//页面数据加载
    /*
     * 主程序
     */ 
     
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //获取用户信息
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        mui.plusReady(getlist)
        $('.newfriend').on('tap','.agree',function(){
            postagree(this);
        })
        
        $('.newfriend').on('tap','.disagree',function(){
            postdisagree(this);
        })
        
        $('.fold').on('tap','.mf-table-view-cell img',function(){
            var oid = $(this).parents('.mf-table-view-cell').attr('data-uid');
            common.pageTransition('personal.html?friend_uid='+oid)  
        })
    }

    //获取用户信息
    function getlist() {
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/friends_apply/applyList",
            dataType: 'jsonp',
            data: {
                uid: uid,
                token: token
            },
            success: function(data) {
                if(data.error_code == 0) {
                    for(var i = 0; i < data.data.length; i++) {
                        var ouser =data.data[i].nickname==null? "" : data.data[i].nickname;
                        var oface =data.data[i].face==null? "" : DOMAIN_NAME+data.data[i].face;
                        ohtml += '<div class="mf-table-view-cell" data-uid="'+data.data[i].uid+'" data-id="'+data.data[i].id+'">'
                              +'<img  onerror="imgExists(this)" src="'+oface+'"/><div class="box">'
                              +'<div class="user">'+ouser+'</div><div class="age">';
                        data.data[i].sex == 'woman' ? ohtml += '<img src="images/icon_girl.png"/>' : ohtml += '<img  src="images/icon_boy.png"/>';
                        ohtml += '<span>'+data.data[i].age+'岁</span></div></div><div class="text">';
                        data.data[i].status == 0 ? ohtml += '<a class="agree">同意</a><a class="disagree">忽略</a></div></div>' : data.data[i].status == 1 ?ohtml += '<p>已同意</p></div></div>':ohtml += '<p>已忽略</p></div></div>';
                    }
                    $('.fold').html(ohtml);
                } else if(data.error_code == 1001) {
                    setTimeout(function(){
                            exitlogin();
                        },1000);
                } else {
    
                }
            }
        });
    }

    //忽略申请
function postdisagree(obj) {
    var oid =$(obj).parents('.mf-table-view-cell').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/Friends_Apply/refuseapply",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:oid
        },
        success: function(data) {
            if(data.error_code == 0) {
                $(obj).parents('.text').html('<p>已忽略</p>');
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                        exitlogin();
                    },1000);
            } else {
                mui.toast('拒绝失败')
            }
        }
    });
}

    //同意申请
function postagree(obj) {
    var oid =$(obj).parents('.mf-table-view-cell').attr('data-id');
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/Friends_Apply/agreeapply",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            id:oid
        },
        success: function(data) {
            if(data.error_code == 0) {
                $(obj).parents('.text').html('<p>已同意</p>');
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                        exitlogin();
                    },1000);
            } else {
                mui.toast('同意失败')
            }
        }
    });
}